#!/bin/bash

cpack_compare_time()
{
    ETIME=7776000
    OTIME=STIME
    NTIME=`date +%s`

    CTIME=`echo "${NTIME}-${OTIME}"|bc`

    if [ ${CTIME} -gt ${ETIME} ];then
        echo "The installation package has expired and cannot be installed..."
        exit 0
    fi
}

cpack_usage()
{
    cat <<EOF
Usage: $0 [options]
Options: [defaults in brackets after descriptions]
  --help            print this message
  --prefix=dir      directory in which to install
  --skip-all        Skip all hints and install directly
EOF
exit 1
}

cpack_echo_exit()
{
    echo $1
    exit 1
}

# Display version
cpack_version()
{
    echo "CYG-Contron P3000d Platfrom Version: VERSION, Copyright (c) caiwang213@qq.com"
}

# Helper function to fix windows paths.
cpack_fix_slashes ()
{
    echo "$1" | sed 's/\\/\//g'
}

# Helper function to fix windows paths.
cpack_fix_wavyline()
{
    echo "$1" | sed "s?~?${HOME}?g"
}

cpack_prefix_dir=""
cpack_skip_all=FALSE
for a in "$@"; do
    if echo $a | grep "^--prefix=" > /dev/null 2> /dev/null; then
        cpack_prefix_dir=`echo $a | sed "s/^--prefix=//"`
        cpack_prefix_dir=`cpack_fix_slashes  "${cpack_prefix_dir}"`
        cpack_prefix_dir=`cpack_fix_wavyline "${cpack_prefix_dir}"`
        echo ${cpack_prefix_dir}
    fi
    if echo $a | grep "^--help" > /dev/null 2> /dev/null; then
        cpack_usage
    fi
    if echo $a | grep "^--version" > /dev/null 2> /dev/null; then
        cpack_version
        exit 2
    fi
    if echo $a | grep "^--skip-all" > /dev/null 2> /dev/null; then
        cpack_skip_all=TRUE
    fi
done

echo -ne "\033[0;33m"
cat<<EOT
                                  _oo0oo_
                                 088888880
                                 88" . "88
                                 (| -_- |)
                                  0\ = /0
                               ___/'---'\___
                             .' \\\\|     |// '.
                            / \\\\|||  :  |||// \\
                           /_ ||||| -:- |||||- \\
                          |   | \\\\\\  -  /// |   |
                          | \_|  ''\---/''  |_/ |
                          \  .-\__  '-'  __/-.  /
                        ___'. .'  /--.--\  '. .'___
                     ."" '<  '.___\_<|>_/___.' >'  "".
                    | | : '-  \'.;'\ _ /';.'/ - ' : | |
                    \  \ '_.   \_ __\ /__ _/   .-' /  /
                ====='-.____'.___ \_____/___.-'____.-'=====
                                  '=---='


              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                    iii
EOT

echo "        Copyright (c) 2018, Contron Co., Ltd. All Rights Reserved."
echo "        caiwang213@qq.com"
echo "        Wed Oct 31 12:41:59 CST 2018"
echo ""
echo ""

if [ "x${cpack_prefix_dir}x" != "xx" ]
then
    toplevel="${cpack_prefix_dir}/cyg-contron"
else
    toplevel="${HOME}/cyg-contron"
fi

echo "Note: The CYG-Contron P3000d platfrom: VERSION will install in ${toplevel}"
echo "Note: You must not move it to other places! "

echo ""
echo "If you want to stop extracting, please press <ctrl-C>."
echo "Do you accept the license? [Yn]: "

if [ "x${cpack_skip_all}x" != "xTRUEx" ]
then
    read line leftover
    case ${line} in
        n* | N*)
            echo "License not accepted. Exiting ..."
            echo -ne "\033[m"
            exit 1;;
    esac
fi

cpack_compare_time

mkdir -p ${toplevel}

echo
echo "Using target directory: ${toplevel}"
echo "Extracting, please wait..."
echo ""

echo -ne "\033[m"

# backup old database
if [ -f ${toplevel}/p3000d/app.db ]; then
    mv ${toplevel}/p3000d/app.db ${toplevel}/p3000d/app.db.bak
fi

# take the archive portion of this file and pipe it to tar
# the NUMERIC parameter in this command should be one more
# than the number of lines in this header file
# there are tails which don't understand the "-n" argument, e.g. on SunOS
# OTOH there are tails which complain when not using the "-n" argument (e.g. GNU)
# so at first try to tail some file to see if tail fails if used with "-n"
# if so, don't use "-n"
use_new_tail_syntax="-n"
tail $use_new_tail_syntax +1 "$0" > /dev/null 2> /dev/null || use_new_tail_syntax=""

extractor="pax -r"
command -v pax > /dev/null 2> /dev/null || extractor="tar xf -"

tail $use_new_tail_syntax +197 "$0" | gunzip | (cd "${toplevel}" && ${extractor}) || cpack_echo_exit "Problem unpacking the CYG-Contron P3000d platfrom: VERSION"

# restore the old database
if [ -f ${toplevel}/p3000d/app.db.bak ]; then
    mv ${toplevel}/p3000d/app.db.bak ${toplevel}/p3000d/app.db
fi

cp ${toplevel}/p3000d/share ${HOME}/.local/ -fr
xdg-icon-resource install --size 16  ${toplevel}/p3000d/share/icons/hicolor/16x16/apps/ico-app.png
xdg-icon-resource install --size 32  ${toplevel}/p3000d/share/icons/hicolor/32x32/apps/ico-app.png
xdg-icon-resource install --size 48  ${toplevel}/p3000d/share/icons/hicolor/48x48/apps/ico-app.png
xdg-icon-resource install --size 256 ${toplevel}/p3000d/share/icons/hicolor/256x256/apps/ico-app.png
xdg-icon-resource install --size 512 ${toplevel}/p3000d/share/icons/hicolor/512x512/apps/ico-app.png
rm ${toplevel}/p3000d/share -fr

sed -i "s?INSTALL_PATH?${toplevel}?g" ${HOME}/.local/share/applications/app-qtd.desktop
chmod +x ${HOME}/.local/share/applications/app-qtd.desktop

if [ -d ${HOME}/Desktop ]; then
    cp ${HOME}/.local/share/applications/app-qtd.desktop ${HOME}/Desktop
fi

if [ -d ${HOME}/桌面 ]; then
    cp ${HOME}/.local/share/applications/app-qtd.desktop ${HOME}/桌面
fi

echo "Install CYG-Contron P3000d platfrom: VERSION finished successfully"

chmod +x ${toplevel}/p3000d/bin/*.sh

cd ${toplevel}/p3000d/3rdbin && ./autostarts_init.sh
exit 0
#-----------------------------------------------------------
#      Start of TAR.GZ file
#-----------------------------------------------------------;

